﻿using System;
using System.Linq;
using youtubeFisher;

namespace Gpf.Whs.YouTubeDownloader.CommSrv
{
    public class YouTubeDownloaderCommSrv : IYouTubeDownloaderCommSrv
    {
        public YouTubeVideoInfo GetFormats(string url)
        {
            var service = YouTubeService.Create(url);
            var info = new YouTubeVideoInfo();
            info.Title = service.VideoTitle;
            info.Formats = (from f in service.AvailableVideoFormat
                            select new YouTubeVideoFormatInfo 
                                       { 
                                           Code = f.FormatCode, 
                                           Title = MapFormatCodeToFilter(f.FormatCode) 
                                       }
                           ).ToArray();

            
            
            return info;
        }

        public void DownloadFile(string url, Guid folderId, byte format)
        {
            var folderPath = WhsHelper.GetFolderPath(folderId);
            DownloadEngine.StartDownload(url, folderPath, format);
        }

        public DownloadItemStatus[] GetItemStatuses()
        {
            return DownloadEngine.GetDownloadStatuses();
        }

        private string MapFormatCodeToFilter(byte formatCode)
        {
            string formatDescription = "";

            switch (formatCode)
            {
                case 43:
                    formatDescription = "WebM Video 360p (*.webm)";
                    break;

                case 44:
                    formatDescription = "WebM Video 480p (*.webm)";
                    break;

                case 45:
                    formatDescription = "WebM HD Video 720p (*.webm)";
                    break;

                case 38:
                    formatDescription = "4K Resolution (*.mp4)";
                    break;

                case 37:
                    formatDescription = "Full HD 1080p (*.mp4)";
                    break;

                case 22:
                    formatDescription = "HD 720p (*.mp4)";
                    break;

                case 82:
                    formatDescription = "3D Standard Youtube Quality 360p (*.mp4)";
                    break;

                case 84:
                    formatDescription = "3D HD 720p (*.mp4)";
                    break;

                case 35:
                    formatDescription = "HQ Flash Video 480p (*.flv)";
                    break;

                case 34:
                    formatDescription = "LQ Flash Video 360p [AAC] (*.flv)";
                    break;

                case 18:
                    formatDescription = "Standard Youtube Quality 360p (*.mp4)";
                    break;

                case 6:
                    formatDescription = "LQ Flash Video [MP3.44KHz] (*.flv)";
                    break;

                case 5:
                    formatDescription = "LQ Flash Video [MP3.22KHz] (*.flv)";
                    break;

                case 13:
                case 17:
                    formatDescription = "Mobile Video (*.3gp)";
                    break;

                default:
                    // New Format?
                    formatDescription = "";
                    break;
            }

            return formatDescription;
        }


        public FolderData[] GetSharedMediaFolders()
        {
            var folders = WhsHelper.GetStreamingMediaFolders();
            var folderData = folders.Select(s => new FolderData { FolderId = s.Key, Name = s.Value });

            return folderData.ToArray();
        }


        public void CancelDownload(string key)
        {
            DownloadEngine.CancelDownload(key);
        }
    }
}
